package com.yunwaikeji.gy_tool_library.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Message;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yunwaikeji.gy_tool_library.info.GYBaseInfo;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;
import com.yunwaikeji.gy_tool_library.utils.GYUtils;

public class GYDialog extends Dialog implements GYBaseInfo {
    public GYDialog(@NonNull Context context) {
        this(context, 0);
    }

    public GYDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected GYDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public GYDialog setSizePx(int widthPx, int heightPx) {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        if (widthPx != UNCHANGED) {
            layoutParams.width = widthPx;
        }
        if (heightPx != UNCHANGED) {
            layoutParams.height = heightPx;
        }
        getWindow().setAttributes(layoutParams);
        return this;
    }

    public int getWidthPx() {
        return getWindow().getAttributes().width;
    }

    public int getHeightPx() {
        return getWindow().getAttributes().height;
    }

    public GYDialog setWidthPx(int widthPx) {
        return setSizePx(widthPx, UNCHANGED);
    }

    public GYDialog setWidthDp(int widthDp) {
        return setWidthPx(GYDisplayUtils.dp2px(getContext(), widthDp));
    }

    public GYDialog setWidthProportion(float widthProportion) {
        return setWidthPx(GYUtils.getWindowWidth(getContext(), widthProportion));
    }

    public GYDialog setHeightPx(int heightPx) {
        return setSizePx(UNCHANGED, heightPx);
    }

    public GYDialog setHeightDp(int heightDp) {
        return setHeightPx(GYDisplayUtils.dp2px(getContext(), heightDp));
    }

    public GYDialog setHeightProportion(float heightProportion) {
        return setHeightPx(GYUtils.getWindowHeight(getContext(), heightProportion));
    }

    public GYDialog setSizeDp(int widthDp, int heightDp) {
        return setSizePx(GYDisplayUtils.dp2px(getContext(), widthDp), GYDisplayUtils.dp2px(getContext(), heightDp));
    }

    public GYDialog setSizeProportion(float width, float height) {
        return setSizePx(GYUtils.getWindowWidth(getContext(), width), GYUtils.getWindowHeight(getContext(), height));
    }

    public GYDialog setSizeRatioFromWidth(int width, int height) {
        return setSizeRatioFromWidthPx(getWidthPx(), width, height);
    }

    public GYDialog setSizeRatioFromWidthPx(int widthPx, int width, int height) {
        return setSizePx(widthPx, widthPx * height / width);
    }

    public GYDialog setSizeRatioFromWidthDp(int widthDp, int width, int height) {
        return setSizeRatioFromWidthPx(GYDisplayUtils.dp2px(getContext(), widthDp), width, height);
    }

    public GYDialog setSizeRatioFromHeight(int width, int height) {
        return setSizeRatioFromHeightPx(getHeightPx(), width, height);
    }

    public GYDialog setSizeRatioFromHeightPx(int heightPx, int width, int height) {
        return setSizePx(heightPx * width / height, heightPx);
    }

    public GYDialog setSizeRatioFromHeightDp(int heightDp, int width, int height) {
        return setSizeRatioFromHeightPx(GYDisplayUtils.dp2px(getContext(), heightDp), width, height);
    }

    public GYDialog setSizeRatioFromWidthProportion(float widthProportion, int width, int height) {
        return setSizeRatioFromWidthPx(GYUtils.getWindowWidth(getContext(), widthProportion), width, height);
    }

    public GYDialog setSizeRatioFromHeightProportion(float heightProportion, int width, int height) {
        return setSizeRatioFromWidthPx(GYUtils.getWindowHeight(getContext(), heightProportion), width, height);
    }

    public GYDialog setCanceledOnTouchOutside1(boolean cancel) {
        super.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public GYDialog setCancelable1(boolean flag) {
        super.setCancelable(flag);
        return this;
    }

    public GYDialog setCancelMessage1(@Nullable Message msg) {
        super.setCancelMessage(msg);
        return this;
    }

    public GYDialog setOnShowListener1(@Nullable OnShowListener listener) {
        super.setOnShowListener(listener);
        return this;
    }

    public GYDialog setOnDismissListener1(@Nullable OnDismissListener listener) {
        super.setOnDismissListener(listener);
        return this;
    }

    public GYDialog setOnKeyListener1(@Nullable OnKeyListener onKeyListener) {
        super.setOnKeyListener(onKeyListener);
        return this;
    }

    public GYDialog setOnCancelListener1(@Nullable OnCancelListener listener) {
        super.setOnCancelListener(listener);
        return this;
    }
}
